//
//  DYPageController.m
//  TimeMall
//
//  Created by 帝云科技 on 2017/12/28.
//  Copyright © 2017年 diyunkeji. All rights reserved.
//

#import "DYPageController.h"

#define kSegmentHeight 43

@interface DYPageController ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@end

@implementation DYPageController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dy_initData {
    [super dy_initData];
}

- (void)dy_initUI {
    [super dy_initUI];
    [self.view addSubview:self.segmentView];
    [self.view addSubview:self.collectionView];
}

#pragma mark - DYPageSegmentViewBlock
- (void)segmentViewSelectedIndex:(NSInteger)selectedIndex {
    CGPoint point = CGPointMake(_collectionView.xhq_width * selectedIndex, 0);
    [_collectionView setContentOffset:point animated:self.animatedOfScrollToOffset];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger seletedIndex = (NSInteger)scrollView.contentOffset.x / scrollView.xhq_width;
    [_segmentView setSelectedIndex:seletedIndex animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    float compare = scrollView.contentOffset.x / scrollView.contentSize.width;
//    [_segmentView downViewDidScroll:compare];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [cell.contentView addSubview:vc.view];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [vc viewWillAppear:YES];
}

#pragma mark - getter
- (DYPageTitleView *)segmentView {
    if (!_segmentView) {
        _segmentView = [[DYPageTitleView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, self.segmentHeight)
                                            withTitleArray:self.titleArray];
        _segmentView.selectedIndex = self.currentIndex;
        @weakify(self);
        _segmentView.block = ^(NSInteger selectedIndex) {
            @strongify(self);
            [self segmentViewSelectedIndex:selectedIndex];
        };
    }
    return _segmentView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - self.segmentHeight - kNavigationStatusHeight);
        
        _collectionView = [[DYPageCollectionView alloc]initWithFrame:CGRectMake(0, kSegmentHeight, kScreenWidth, kScreenHeight - kNavigationStatusHeight - self.segmentHeight) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.bounces = NO;
        _collectionView.scrollEnabled = YES;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        if (_currentIndex > 0) {
            [self segmentViewSelectedIndex:self.currentIndex];
        }
    }
    return _collectionView;
}

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [[NSArray alloc]init];
    }
    return _titleArray;
}

- (CGFloat)segmentHeight {
    if (!_segmentHeight) {
        _segmentHeight = kSegmentHeight;
    }
    return _segmentHeight;
}

- (NSInteger)currentIndex {
    if (!_currentIndex) {
        _currentIndex = 0;
    }
    return _currentIndex;
}

@end
